package com.neuedu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/config")
@RefreshScope
public class NacosConfigController {

    //1、硬编码方式
    @Autowired
    private ConfigurableApplicationContext applicationContext;

    @GetMapping("/test-config1")
    public String nacosConfigTest1() {
        return applicationContext.getEnvironment().getProperty("config.appName");
    }

    //2、动态读取方式
    @Value("${config.appName}")
    private String appName;

//    @Value("${product.price-strategy.1}")
//    private float price1;
//
//    @Value("${product.price-strategy.2}")
//    private float price2;

    //
    @GetMapping("/test-config2")
    public String nacosCongifTest2() {
        return appName;
    }

    @Value("${config.env}")
    private String env;

    @GetMapping("/test-config3")
    public String nacosConfigTest3() {
        return env;
    }

    // 获取商品价格策略
    @GetMapping("/product-price-strategy/{pid}")
    public String getProductPriceStrategy(@PathVariable("pid") String productId) {
        String price = applicationContext.getEnvironment().getProperty("product.price-strategy." + productId);
        if (price != null) {
            return "商品 ID 为 " + productId + " 的价格策略为：" + price;
        } else {
            return "未找到商品 ID 为 " + productId + " 的价格策略";
        }
    }


}

